<template>
  <j-modal
    :title="title"
    :width="400"
    :visible="visible"
    :maskClosable="false"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel">
    <a-spin :spinning="returnConfirm">
      <a-form-model ref="confirmForm" :model="record" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="库位" :labelCol="{ xs: { span: 24 }, sm: { span: 5 }}"  :wrapperCol="{ xs: { span: 24 }, sm: { span: 16 }}" prop="storageLocationId">
              <j-search-select-tag type="list" v-model="record.storageLocationId" :dict="locationItemDict" placeholder="请选择库位" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="批次" :labelCol="{ xs: { span: 24 }, sm: { span: 5 }}"  :wrapperCol="{ xs: { span: 24 }, sm: { span: 16 }}" prop="batchNo">
              <a-input type="list" v-model="record.batchNo" placeholder="请填写批次" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import {httpAction, putAction} from "@/api/manage";

  export default {
    name: 'SdSalesReturnDetailConfirmModal',
    components: {
    },
    data() {
      return {
        title:'销售退货',
        width:800,
        visible: false,
        disableSubmit: false,
        returnConfirm: false,
        validatorRules: {
          storageLocationId: [
            { required: true, message: '请选择库位!'},
          ],
          batchNo: [
            { required: true, message: '请填写批次!'},
          ],
        },
        record:{},
        locationItemDict: '',
        url:{
          handleConfirm: "/sd/salesReturn/handleConfirm",
        }
      }
    },
    computed:{
    },
    methods:{
      handleConfirm (record) {
        this.visible=true
        this.$nextTick(()=>{
          this.record = record;
          this.locationItemDict = "mdm_storage_location where warehouse_id = '"+ record.warehouseId +"',location_name,id";
          if(record.materialBatchFlag === '1'){
            this.validatorRules.batchNo[0].required = true;
          }else{
            this.validatorRules.batchNo[0].required = false;
          }
          if(record.locationFlag === '1'){
            this.validatorRules.storageLocationId[0].required = true;
          }else{
            this.validatorRules.storageLocationId[0].required = false;
          }
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.confirmForm.validate(valid => {
          if (valid) {
            that.returnConfirm = true;
            putAction(this.url.handleConfirm, [that.record]).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
              this.visible = false;
            }).finally(() => {
              that.returnConfirm = false;
            })
          }
        })
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>

<style scoped>
</style>